//
//  QZUserInfoModel.m
//  shortvideoplay
//
//  Created by tikeios_002 on 2024/6/12.
//

#import "QZUserInfoModel.h"
#import "DJNavigationController.h"
#import "QZTabBarController.h"
#import "QZLoginVC.h"
#import "QZOtherMyVC.h"
#import "CSJADLoading.h"

@implementation QZUserInfoModel
+ (instancetype)share {
    static QZUserInfoModel *manager;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[QZUserInfoModel alloc] init];
    });
    return manager;
}
- (NSMutableArray *)adSaveIdArr{
    if(_adSaveIdArr == nil){
        _adSaveIdArr = [NSMutableArray new];
    }
    return _adSaveIdArr;
}
- (NSMutableArray *)adSaveFishArr{
    if(_adSaveFishArr == nil){
        _adSaveFishArr = [NSMutableArray new];
    }
    return _adSaveFishArr;
}
- (void)setValue:(id)value forUndefinedKey:(NSString *)key{}

+ (BOOL)isLogin {
    id user = USERDEFAULT_GetValue(QZUserInfoKey);
    if (user == nil || ![user isKindOfClass:[NSDictionary class]] || [user count] == 0) return NO;
    return YES;
}
#pragma mark -清除登录用户数据
+ (void)cleanUserData{
    // 用户信息
    [QZUserInfoModel share].onceLogoutAlert = NO;
    [[NSUserDefaults standardUserDefaults]removeObjectForKey:QZUserInfoKey];
    
    [[NSUserDefaults standardUserDefaults] synchronize];
    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
    // tabbar切换
    [self tabbarChangeIndex:0];
 
}
+ (void)tabbarChangeIndex:(NSInteger)index {
    DJNavigationController *nav = nil;
    QZTabbarController *tabBarVC = nil;
    if ([[[UIApplication sharedApplication].delegate window].rootViewController isKindOfClass:[DJNavigationController class]]) {
        nav = (DJNavigationController *)[[UIApplication sharedApplication].delegate window].rootViewController;
        [nav popToRootViewControllerAnimated:NO];
        NSArray *rootControllerChilds = nav.childViewControllers;
        for (UIViewController *child in rootControllerChilds) {
            if ([child isKindOfClass:[QZTabbarController class]]) {
                tabBarVC = (QZTabbarController *)child;
            }
        }
        if (tabBarVC != nil) {
            if (tabBarVC.selectedIndex == index) {
                return;
            }
            NSArray *itemControllers = tabBarVC.viewControllers;
            for (UIViewController *_controller in itemControllers) {
                if ([_controller isKindOfClass:[DJNavigationController class]]) {
                    [((DJNavigationController *)_controller) popToRootViewControllerAnimated:NO]; //关闭动画解决偶尔出现的tabbar消失的情况
                }else if ([_controller isKindOfClass:[UIViewController class]]){
                    [_controller dismissViewControllerAnimated:NO completion:nil];
                }
            }
        } else {
            tabBarVC = [QZTabbarController new];
            nav = [[DJNavigationController alloc] initWithRootViewController:tabBarVC];
            [nav setNavigationBarHidden:YES animated:YES];
        }
        
    } else {
        tabBarVC = [QZTabbarController new];
        nav = [[DJNavigationController alloc] initWithRootViewController:tabBarVC];
        [nav setNavigationBarHidden:YES animated:YES];
    }
    tabBarVC.selectedIndex = index;
    [tabBarVC setDefaultSelectIndex:index];
}
+ (void)otherMineControlView:(UIViewController *)vc mineId:(id)mid videoId:(id _Nullable)videoId{
    [QZHongBaoView share].hidden = YES;
    [[QZHongBaoView share] stopClick:NO];
    QZOtherMyVC * other = [[QZOtherMyVC alloc]init];
    other.otherMid = mid;
    other.otherVid = videoId;
    if([Tools currentActiveController].navigationController == nil){
        DJNavigationController * nav = [[DJNavigationController alloc]initWithRootViewController:other];
        nav.delegate = [DJBarDelegate share];
        other.ispresend = YES;
        nav.modalPresentationStyle = UIModalPresentationFullScreen;
        [vc presentViewController:nav animated:YES completion:nil];
    }else{
        [[Tools currentActiveController].navigationController pushViewController:other animated:YES];
    }
}
+ (void)presentLoginView{
    QZLoginVC * login = [[QZLoginVC alloc]init];
    login.modalPresentationStyle = UIModalPresentationFullScreen;
    [[Tools currentActiveController] presentViewController:login animated:YES completion:nil];
}
+ (void)addPlayNuber:(id)videoId{
    KMutableDic(sendDic)
    sendDic[@"videoid"] = videoId;
    [YBNetBase incPvRequest:sendDic successBlock:^(NSDictionary * _Nonnull data, NSInteger code, NSString * _Nonnull msg) {
        
    }];
}
+ (void)addHisPlay:(id)videoId durations:(CGFloat)time isPlayEnd:(NSInteger)playEnd{
    if([QZUserInfoModel isLogin] == NO){
        return;
    }
    KMutableDic(sendDic)
    sendDic[@"videoid"] = videoId;
    sendDic[@"durations"] = [NSString stringWithFormat:@"%0f",time];
    sendDic[@"is_play_end"] = @(playEnd);
    [YBNetBase addHisPlayRequest:sendDic successBlock:^(NSDictionary * _Nonnull data, NSInteger code, NSString * _Nonnull msg) {
        
    }];
}
- (NSMutableArray *)playFishEndIdArr{
    if(_playFishEndIdArr == nil){
        _playFishEndIdArr = [NSMutableArray new];
    }
    return _playFishEndIdArr;
}
+ (void)adRequestType:(void (^)(NSInteger type))adType{
    if([CSJADLoading share].isCSJAdInit && [CSJADLoading share].isYLHAdInit){
        NSInteger number = arc4random_uniform(2);
        adType(number);
    }else if([CSJADLoading share].isCSJAdInit == NO && [CSJADLoading share].isYLHAdInit == NO){
        [CSJADLoading initAppCSJAD_successBlock:^(BOOL issuccess) {
            [CSJADLoading initAppYLHAD_successBlock:^(BOOL issuccess) {
                [QZUserInfoModel adRequestType:^(NSInteger type) {
                    adType(type);
                }];
            }];
        }];
    }else{
        if([CSJADLoading share].isCSJAdInit){
            adType(0);
        }else if([CSJADLoading share].isYLHAdInit){
            adType(1);
        }
    }
}
@end
